package com.myrock.zlbandy.gorock.rock.ui;


import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.etsy.android.grid.StaggeredGridView;
import com.myrock.zlbandy.gorock.HttpModel.Model.ImageInfo;
import com.myrock.zlbandy.gorock.rock.z.MyRockAPI;
import com.myrock.zlbandy.gorock.rock.tools.NetRequest;
import com.myrock.zlbandy.gorock.HttpModel.ParseInfo;
import com.myrock.zlbandy.gorock.HttpModel.Model.RockInfo;
import com.myrock.zlbandy.gorock.rock.tools.LruImageCache;
import com.myrock.zlbandy.gorock.R;
import com.myrock.zlbandy.gorock.my.adapter.PhotoAdapter;
import com.myrock.zlbandy.gorock.my.views.ScaleImageView;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class PhotoFragment extends Fragment implements AbsListView.OnItemClickListener{
    private StaggeredGridView gridView;
    private  RockInfo rockInfo;
    private ProgressBar myProgressBar;
    private TextView textView;
    private TextView txtFooterTitle;
    private ArrayList<ImageInfo> imageInfos = new ArrayList<ImageInfo>();
    private Context context;



    public PhotoFragment() {
        // Required empty public constructor
    }

    public void init(RockInfo rockInfo){
        this.rockInfo = rockInfo;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View photo = inflater.inflate(R.layout.phone_layout, container,false);
        context = photo.getContext();
        gridView = (StaggeredGridView)photo.findViewById(R.id.rock_view);
        View footer = inflater.inflate(R.layout.list_item_header_footer, null);
        textView = (TextView)photo.findViewById(R.id.tv);
        myProgressBar = (ProgressBar)photo.findViewById(R.id.progressBar);
        txtFooterTitle =  (TextView) footer.findViewById(R.id.txt_title);
        txtFooterTitle.setText("更多图片正在制作中....");
        gridView.addFooterView(footer);
        gridView.setOnItemClickListener(this);
        ImageTask imageTask = new ImageTask(myProgressBar,textView,photo.getContext());
        imageTask.execute(rockInfo.getId());
        return photo;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View imgEntryView = inflater.inflate(R.layout.bigimage, null); // 加载自定义的布局文件
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        ScaleImageView bm = (ScaleImageView)imgEntryView.findViewById(R.id.big_image);
        LruImageCache lruImageCache = LruImageCache.instance();
        ImageLoader imageLoader = new ImageLoader(NetRequest.getInstance(context),lruImageCache);
        bm.setImageUrl(MyRockAPI.IMAGE_API+imageInfos.get(position).getId()+".jpg", imageLoader);
        dialog.setView(imgEntryView); // 自定义dialog
        dialog.show();

        bm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
    }


    //内部类异步加载

    class ImageTask extends AsyncTask<String, Integer, ArrayList<ImageInfo>> {

        ProgressBar myprogressbar = null;
        //MainActivity的文本
        TextView textView = null;
        // MainActivity的上下文
        Context mycontext = null;
        //onPreExecute方法用于在执行后台任务前做一些UI操作

        public ImageTask(ProgressBar progressbar, TextView textview, Context context) {
            myprogressbar = progressbar;
            mycontext = context;
            textView = textview;
        }


        @Override
        protected void onPreExecute() {
            myprogressbar.setVisibility(View.VISIBLE);
            textView.setText("正在加载...");
            myprogressbar.setProgress(10);


        }

        //doInBackground方法内部执行后台任务,不可在此方法内修改UI
        @Override
        protected ArrayList<ImageInfo> doInBackground(String... params) {
            ArrayList<ImageInfo> arrayList = new ArrayList<ImageInfo>();
            arrayList = ParseInfo.GetImage(params[0]);
            publishProgress(50);
            for(int i = 50;i<99;i++){
                publishProgress(i);
            }

            return arrayList;

        }

        //onProgressUpdate方法用于更新进度信息
        @Override
        protected void onProgressUpdate(Integer... progresses) {
            int progress = myprogressbar.getMax() / 100 * (progresses[0] + 1);
            myprogressbar.setProgress(progress);
            textView.setText("正在加载..." + progress + "%");

        }

        //onPostExecute方法用于在执行完后台任务后更新UI,显示结果
        @Override
        protected void onPostExecute(ArrayList<ImageInfo> result) {
            myprogressbar.setVisibility(View.GONE);
            textView.setVisibility(View.GONE);
            imageInfos = result;
            if(result.size()>0) {
                imageInfos.remove(0);
                PhotoAdapter adapter = new PhotoAdapter(mycontext, R.layout.photo_grid,
                        imageInfos, new int[]{R.id.imageView1});
                gridView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }else
            {
                Toast.makeText(mycontext,"数据异常，请稍后再试",Toast.LENGTH_SHORT).show();
            }

        }

        //onCancelled方法用于在取消执行中的任务时更改UI
        @Override
        protected void onCancelled() {

        }
    }
}
